package domain

import (
	"AcsGo/internal/util"
	"fmt"
)

type BookReturnRequest struct {
	Code             util.Field
	Fill             util.Field
	TransactionDate  util.Field
	ReturnDate       util.Field
	CurrentLocation  util.FieldPrefix
	InstitutionId    util.FieldPrefix
	ItemIdentifier   util.FieldPrefix
	TerminalPassword util.FieldPrefix
	Cancel           util.FieldPrefix
	Operator         util.FieldPrefix
	SequenceNumber   util.FieldPrefix
}

func HandleBookReturnRequest(str string) (*BookReturnRequest, error) {
	if str == "" {
		return nil, fmt.Errorf("str is empty")
	}
	var query = BookReturnRequest{
		Code:             util.Field{Value: "09", Length: 2},
		Fill:             util.Field{Value: "N", Length: 1},
		TransactionDate:  util.Field{Value: "", Length: 18},
		ReturnDate:       util.Field{Value: "", Length: 18},
		CurrentLocation:  util.FieldPrefix{Value: "", Prefix: "AP", Suffix: util.PrefixLink},
		InstitutionId:    util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		ItemIdentifier:   util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		TerminalPassword: util.FieldPrefix{Value: "", Prefix: "AC", Suffix: util.PrefixLink},
		Cancel:           util.FieldPrefix{Value: "", Prefix: "BI", Suffix: util.PrefixLink},
		Operator:         util.FieldPrefix{Value: "", Prefix: "CN", Suffix: util.PrefixLink},
	}

	length := query.Code.Length + query.Fill.Length + query.TransactionDate.Length
	if len(str) < length {
		return nil, fmt.Errorf("str is too short")
	}

	strTemp := query.Code.GetValue(str)
	strTemp = query.Fill.GetValue(strTemp)
	strTemp = query.TransactionDate.GetValue(strTemp)
	strTemp = query.CurrentLocation.GetValue(strTemp)
	strTemp = query.InstitutionId.GetValue(strTemp)
	strTemp = query.ItemIdentifier.GetValue(strTemp)
	strTemp = query.TerminalPassword.GetValue(strTemp)
	strTemp = query.Cancel.GetValue(strTemp)
	strTemp = query.Operator.GetValue(strTemp)
	_ = query.SequenceNumber.GetValue(strTemp)
	return &query, nil
}

type BookReturnResponse struct {
	Code              util.Field
	OK                util.Field
	Fill              util.Field
	TransactionDate   util.Field
	InstitutionId     util.FieldPrefix
	PatronIdentifier  util.FieldPrefix
	PersonalName      util.FieldPrefix
	ItemIdentifier    util.FieldPrefix
	TitleIdentifier   util.FieldPrefix
	PermanentLocation util.FieldPrefix
	ItemStatus        util.FieldPrefix
	MediaType         util.FieldPrefix
	ItemCirtype       util.FieldPrefix
	ItemProperties    util.FieldPrefix
	Fee               util.FieldPrefix
	CallNo            util.FieldPrefix
	SortBin           util.FieldPrefix
	ScreenMessage     util.FieldPrefix
	PrintLine         util.FieldPrefix
	SequenceNumber    util.FieldPrefix
}

func (q BookReturnResponse) StringFormat() (string, error) {
	sb, err := util.ConcatenateFields(q)
	if err != nil {
		return "", err
	}
	return sb, nil
}

func InitBookReturnResponse(success, rdbm, rdname, bkbm, gox, Indexnb, Cirtype, bkTitle, address, price string) *BookReturnResponse {
	var queryResponse = BookReturnResponse{
		Code:              util.Field{Value: "10", Length: 2},
		OK:                util.Field{Value: "1", Length: 1},
		Fill:              util.Field{Value: "YNN", Length: 3},
		TransactionDate:   util.Field{Value: util.GetCurrentTimeInFormat(), Length: 18},
		InstitutionId:     util.FieldPrefix{Value: "", Prefix: "AO", Suffix: util.PrefixLink},
		PatronIdentifier:  util.FieldPrefix{Value: "", Prefix: "AA", Suffix: util.PrefixLink},
		PersonalName:      util.FieldPrefix{Value: "", Prefix: "AE", Suffix: util.PrefixLink},
		ItemIdentifier:    util.FieldPrefix{Value: "", Prefix: "AB", Suffix: util.PrefixLink},
		TitleIdentifier:   util.FieldPrefix{Value: "", Prefix: "AJ", Suffix: util.PrefixLink},
		PermanentLocation: util.FieldPrefix{Value: "", Prefix: "AQ", Suffix: util.PrefixLink},
		ItemStatus:        util.FieldPrefix{Value: "", Prefix: "ST", Suffix: util.PrefixLink},
		MediaType:         util.FieldPrefix{Value: "001", Prefix: "CK", Suffix: util.PrefixLink},
		ItemCirtype:       util.FieldPrefix{Value: "", Prefix: "CT", Suffix: util.PrefixLink},
		ItemProperties:    util.FieldPrefix{Value: "", Prefix: "CH", Suffix: util.PrefixLink},
		Fee:               util.FieldPrefix{Value: "", Prefix: "CF", Suffix: util.PrefixLink},
		CallNo:            util.FieldPrefix{Value: "", Prefix: "KC", Suffix: util.PrefixLink},
		SortBin:           util.FieldPrefix{Value: "sort bin A1", Prefix: "CL", Suffix: util.PrefixLink},
		ScreenMessage:     util.FieldPrefix{Value: "", Prefix: "AF", Suffix: util.PrefixLink},
		PrintLine:         util.FieldPrefix{Value: "", Prefix: "AG", Suffix: util.PrefixLink},
		SequenceNumber:    util.FieldPrefix{Value: "2AZA3FF", Prefix: "AY", Suffix: util.PrefixLink},
	}
	queryResponse.OK.Value = success
	queryResponse.PatronIdentifier.Value = rdbm
	queryResponse.PersonalName.Value = rdname
	queryResponse.ItemIdentifier.Value = bkbm
	queryResponse.TitleIdentifier.Value = bkTitle
	queryResponse.PermanentLocation.Value = address
	queryResponse.ItemStatus.Value = gox
	queryResponse.ItemCirtype.Value = fmt.Sprintf("HL_CN%s", Cirtype)
	queryResponse.ItemProperties.Value = fmt.Sprintf("ATY%s", price)
	queryResponse.CallNo.Value = Indexnb

	if success == "1" {
		queryResponse.ScreenMessage.Value = fmt.Sprintf("图书[%s]还书处理成功", bkbm)
		queryResponse.PrintLine.Value = fmt.Sprintf("图书[%s]还书处理成功", bkbm)
	} else {
		queryResponse.ScreenMessage.Value = fmt.Sprintf("图书[%s]还书处理失败 图书超期 请联系管理员", bkbm)
		queryResponse.PrintLine.Value = fmt.Sprintf("图书[%s]还书处理失败 图书超期  请联系管理员", bkbm)
	}
	return &queryResponse
}
